//package com.jhhc.Config;
//
//import java.io.BufferedReader;
//import java.io.ByteArrayInputStream;
//import java.io.ByteArrayOutputStream;
//import java.io.IOException;
//import java.io.InputStream;
//import java.io.InputStreamReader;
//
//import javax.servlet.Filter;
//import javax.servlet.FilterChain;
//import javax.servlet.FilterConfig;
//import javax.servlet.ReadListener;
//import javax.servlet.ServletException;
//import javax.servlet.ServletInputStream;
//import javax.servlet.ServletRequest;
//import javax.servlet.ServletResponse;
//import javax.servlet.annotation.WebFilter;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletRequestWrapper;
//
//import org.springframework.stereotype.Component;
//import com.google.common.base.Charsets;
//
///**
// * HTTP请求过滤器<br>
// * 将{@link HttpServletRequest}封装为{@link BufferedRequestWrapper}替换掉整个请求链路中的Request。
// *
// * @author guyadong
// *
// */
//@Component
//@WebFilter(urlPatterns = "/*", filterName = "bufferedRequestFilter")
//public class BufferedRequestFilter implements Filter {
//
//    @Override
//    public void init(FilterConfig filterConfig) throws ServletException {
//
//    }
//
//    @Override
//    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
//            throws IOException, ServletException {
//        if (servletRequest instanceof HttpServletRequest && !(servletRequest instanceof BufferedRequestWrapper)) {
//            /** 将请求封装为BufferedRequestWrapper传递下去 */
//            HttpServletRequestWrapper requestWrapper = new BufferedRequestWrapper((HttpServletRequest) servletRequest);
//            filterChain.doFilter(requestWrapper, servletResponse);
//        } else {
//            filterChain.doFilter(servletRequest, servletResponse);
//        }
//    }
//
//    @Override
//    public void destroy() {
//
//    }
//
//    /**
//     * 基于 HttpServletRequestWrapper封装，允许 HTTP请求中的{@link InputStream}被重复读取
//     */
//    private static class BufferedRequestWrapper extends HttpServletRequestWrapper {
//        private final byte[] buffer;
//
//        public BufferedRequestWrapper(HttpServletRequest request) throws IOException {
//            super(request);
//            /** 读取InputSteam中的数据保存到 buffer */
//            InputStream is = request.getInputStream();
//            ByteArrayOutputStream os = new ByteArrayOutputStream();
//            byte[] buf = new byte[1024];
//            int readCount;
//            while ((readCount = is.read(buf)) > 0) {
//                os.write(buf, 0, readCount);
//            }
//            buffer = os.toByteArray();
//        }
//
//        @Override
//        public ServletInputStream getInputStream() {
//            return new BufferedServletInputStream(buffer);
//        }
//
//        @Override
//        public BufferedReader getReader() throws IOException {
//            return new BufferedReader(new InputStreamReader(this.getInputStream(), Charsets.UTF_8));
//        }
//
//    }
//
//    private static class BufferedServletInputStream extends ServletInputStream {
//        private final ByteArrayInputStream inputStream;
//
//        public BufferedServletInputStream(byte[] buffer) {
//            this.inputStream = new ByteArrayInputStream(buffer);
//        }
//
//        @Override
//        public int read() throws IOException {
//            return inputStream.read();
//        }
//
//        @Override
//        public boolean isFinished() {
//            return inputStream.available() == 0;
//        }
//
//        @Override
//        public boolean isReady() {
//            return !isFinished();
//        }
//
//        @Override
//        public void setReadListener(ReadListener readListener) {
//            // Not implemented
//        }
//    }
//}